<template>
	
	<div class="home-box" style="margin-left: 65px">
	  <el-form :inline="true" :model="PageForm">
	    <el-form-item label="特定绩效项目">
	      <el-input v-model="PageForm.fileName" placeholder="特定绩效项目" />
	    </el-form-item>
	    <el-form-item label="员工姓名">
	      <el-input
	        v-model="PageForm.empName"
	        placeholder="员工姓名"
	        clearable
	      ></el-input>
	    </el-form-item>
		<el-form-item label="是否保存">
		  <el-select v-model="PageForm.save" placeholder="请选择">
		      <el-option
		        label="是"
		        value="1">
		      </el-option>
			  <el-option
			    label="否"
			    value="0">
			  </el-option>
			  <el-option
			    key="2"
			    label="全部"
			    value="2">
			  </el-option>
		    </el-select>
			
		</el-form-item>
		
	    <el-form-item>
	      <el-button @click="searchEvent" type="primary">查询 </el-button>
	    </el-form-item>
	    <el-form-item>
	      <el-button @click="update" type="primary">重置 </el-button>
	    </el-form-item>
	  </el-form>
	</div>
	
	
	<el-button style="margin-left: 65px" type="success" plain icon="Check" @click="handleCopy"
	  >复制上月</el-button
	>
	
  <el-button
    style="margin-left: 10px"
    color="#626aef"
    plain
    icon="Upload"
    @click="handleUP"
  >
    导入
  </el-button>
  
  <el-button
    color="#626aef"
    style="margin-left: 10px"
    :dark="isDark"
    plain
    icon="Download"
    @click="handleDown"
    >导出</el-button
  >
  
  <el-button type="success" plain icon="Download" @click="handleFileAll"
  >一键归档</el-button>
  
  <el-tooltip class="item" effect="dark" content="刷新" placement="top">
    <el-button circle icon="Refresh" @click="update" />
  </el-tooltip>
  <el-table
    :data="EmpPieceList"
    border
    style="
      width: 90%;
      margin: 20px auto;
      border-radius: 8px;
      box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
    "
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
  >
    <el-table-column
      prop="fileName"
      label="特定绩效项目"
      width="200"
    ></el-table-column>
    <el-table-column
      prop="empName"
      label="员工姓名"
      width="200"
    ></el-table-column>
    <el-table-column
      prop="reward"
      label="奖励/￥"
      width="200"
    ></el-table-column>
	
	<el-table-column
	  prop="state"
	  label="状态"
	  width="200"
	>
	<template #default="scope">
	  <div style="display: flex; align-items: center">
	    <span style="margin-left: 10px">{{ scope.row.state == 1?"未归档":"已归档"}}</span>
	  </div>
	</template>
	</el-table-column>
	
	<el-table-column
	  prop="state"
	  label="保存"
	  width="200"
	>
	<template #default="scope">
	  <div style="display: flex; align-items: center">
	    <span style="margin-left: 10px">{{ scope.row.save == 1?"是":"否"}}</span>
	  </div>
	</template>
	</el-table-column>
	
    <el-table-column label="最后更新时间">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)" v-if="scope.row.state == 1"
          >编辑</el-button
        >
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <pagination
    class="pagination"
    v-show="EmpPieceListPage.total > 0"
    :total="EmpPieceListPage.total"
    v-model:page="PageForm.page"
    v-model:limit="PageForm.page_size"
    @pagination="handlePagination"
  />
  <Dialog
    v-model="dialogFormVisible"
    :CadreList="CadreList"
    :close="close"
    :update="update"
    :selectContent="selectContent"
    :type="type"
    :title="title"
  ></Dialog>
  <UploadExcel
    v-model="upload.open"
    :title="upload.upLoadTitle"
    :url="importUrl"
    :tipShow="true"
    @onSuccess="onSuccess"
    @onDownTemplate="onDownTemplate"
	@onError="onError"
  ></UploadExcel>
</template>
<script setup>
import {
  ref,
  onMounted,
  onBeforeMount,
  reactive,
  defineEmits,
  watch,
} from "vue";
import { getEmpRewardPage, deleteOneReward , fileAll,search,copy} from "@/api/empReward";
import UploadExcel from "@/components/SendAndDownload/index";
import Dialog from "./components/rewardWageDialog";
import { ElMessage } from "element-plus";
import { getRewardTemplate } from "@/api/template";
import {downLoadEmpReward} from "@/api/downExcel";

const PageForm = ref({
	fileName:"",
	empName:"",
	save:"",
  page: 1,
  page_size: 10,
});

const searchEvent = () => {
  search(PageForm.value).then((data) => {
    EmpPieceListPage.value = data.data;
    EmpPieceList.value = data.data.records;
  });
};


const EmpPieceListPage = ref([]);
const EmpPieceList = ref([]);
onBeforeMount(() => {
  getEmpRewardPage(PageForm.value).then((data) => {
    EmpPieceListPage.value = data.data;
    EmpPieceList.value = data.data.records;
  });
});
watch(EmpPieceList, (newVal) => {}, { deep: true });
const handlePagination = (value) => {
	
	if (PageForm.value.fileName == "" && PageForm.value.empName == ""&&PageForm.value.save == "") {
	  getEmpRewardPage(PageForm.value).then((data) => {
	    EmpPieceListPage.value = data.data;
	    EmpPieceList.value = data.data.records;
	  });
	} else {
	  PageForm.value.page = value.page;
	  PageForm.value.page_size = value.page_size;
	  search(PageForm.value).then((data) => {
	    EmpPieceListPage.value = data.data;
	    EmpPieceList.value = data.data.records;
	  });
	}
	
  
};

const importUrl = ref("/auth/excel/employeeReward/upload");
const upload = ref({
  open: false,
  upLoadTitle: "特殊绩效上传",
});

const onSuccess = (res) => {
  getEmpRewardPage(PageForm.value).then((data) => {
	  
    EmpPieceListPage.value = data.data;
    EmpPieceList.value = data.data.records;
  });
};

const onError = (rep) =>{
	
}


const handleUP = () => {
  upload.value.open = true;
};

const onDownTemplate = () => {
  getRewardTemplate();
};



const handleDown = (index, row) => {
  downLoadEmpReward().then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "导出成功",
        type: "success",
      });
      update();
    } else {
      ElMessage.error("导出失败");
    }
  });
};

const title = ref("新增特殊绩效");
const type = ref("add");
const selectContent = ref();
const dialogFormVisible = ref(false);
const handleEdit = (index, row) => {
  selectContent.value = row;
  title.value = "编辑特殊绩效";
  type.value = "edit";
  dialogFormVisible.value = true;
};
const handleDelete = (index, row) => {
  delete row.createTime;
  delete row.updateTime;
  row.id = row.empRewardId;
  deleteOneReward(row).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "删除成功",
        type: "success",
      });
      update();
    } else {
      ElMessage.error("删除失败");
    }
  });
};
const close = () => {
  dialogFormVisible.value = false;
};
const update = () => {
	
	  PageForm.value.fileName = "";
	  PageForm.value.empName = "";
	  PageForm.value.save = "";
	  
  getEmpRewardPage(PageForm.value).then((data) => {
    EmpPieceListPage.value = data.data;
    EmpPieceList.value = data.data.records;
  });
};

const emits = defineEmits(["update:modelValue", "onSuccess", "onDownTemplate"]);

const handleCopy = () => {
  copy().then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "复制成功",
        type: "success",
      });
      update();
    } else {
      ElMessage.error("复制失败");
    }
  });
  update();
};

const handleFileAll=()=>{
  fileAll().then(data=>{
    if (data.code == 200) {
      ElMessage({
        message:"归档成功",
        type: "success",
      });
      update();
    } 
    else {
      ElMessage.error("归档失败");
    }
  })
}


</script>

<style lang="scss" scoped>
.title1 {
  color: rgb(64, 158, 255);
  width: 300px;
  height: 45px;
  font-size: 25px;
  background: rgba(64, 158, 255, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 10px auto 10px;
  line-height: 45px;
  border-top: #409eff 2px solid;
  border-bottom: #409eff 2px solid;
  box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
}
.pagination {
  margin: 20px 50px;
  display: flex;
  justify-content: end;
}
</style>
